package blog.spring.db.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import blog.core.exception.FormParamErrorException;
import blog.core.exception.NotFoundException;
import blog.spring.db.dao.IUserDAO;
import blog.spring.db.entity.User;
import blog.spring.db.service.IUserService;

@Service("userService")
public class UserService extends BaseService<User> implements IUserService {

	private final IUserDAO userDao;
	public static final String TYPE_NAME = "用户";

	@Autowired
	public UserService(IUserDAO userDao) {
		super(userDao, TYPE_NAME);
		this.userDao = userDao;
	}

	@Override
	public void save(User entity) throws FormParamErrorException {
		// 注册的逻辑处理
		if (entity.getCmsUser() == null)
			throw new FormParamErrorException("不包含用户类型");
		super.save(entity);
	}

	@Override
	public void update(User entity) throws FormParamErrorException,
			NotFoundException {
		if (super.findByNameIsExist(entity.getUserName()))
			throw new FormParamErrorException("用户名已经存在");
		super.update(entity);
	}

	@Override
	public User login(String username, String password)
			throws FormParamErrorException {
		if (!super.findByNameIsExist(username))
			throw new FormParamErrorException("用户名不存在");
		User tempUser = userDao.login(username, password);
		if (tempUser == null)
			throw new FormParamErrorException("密码错误");
		return tempUser;
	}

	@Override
	public void addSysUser(User user) throws FormParamErrorException {
		if (user.getSysUser() == null)
			throw new FormParamErrorException("不包含用户类型");
		super.save(user);
	}

}
